package com.jbit.dm212.service.impl;

import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.jbit.dm212.entity.dto.DmItemCommentDTO;
import com.jbit.dm212.entity.po.DmImage;
import com.jbit.dm212.entity.po.DmItemComment;
import com.jbit.dm212.mapper.DmItemCommentMapper;
import com.jbit.dm212.service.DmItemCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class DmItemCommentServiceImpl implements DmItemCommentService {
    @Resource
    private DmItemCommentMapper itemCommentMapper;
    @Override
    public List<DmItemCommentDTO> getCommentsByItemId(Integer itemId) {
        MPJLambdaWrapper<DmItemComment> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.select(
                DmItemComment::getItemId,
                DmItemComment::getUserId,
                DmItemComment::getScore,
                DmItemComment::getContent,
                DmItemComment::getCreatedTime
        );
        lambdaWrapper.select(DmImage::getImgUrl);
//        lambdaWrapper.eq(DmImage::getType,0).eq(DmImage::getCategory,0);
        lambdaWrapper.eq(DmItemComment::getItemId,itemId);

        lambdaWrapper.leftJoin(DmImage.class,wrapper->{
            wrapper.eq(DmImage::getType,0).eq(DmImage::getCategory,0);
            wrapper.eq(DmImage::getTargetId,DmItemComment::getUserId);
            return wrapper;
        });
//        lambdaWrapper.leftJoin(DmImage.class,DmImage::getTargetId,DmItemComment::getUserId);
        return itemCommentMapper.selectJoinList(DmItemCommentDTO.class,lambdaWrapper);
    }

    @Override
    public boolean addComment(DmItemComment itemComment) {
        return itemCommentMapper.insert(itemComment) == 1;
    }
}
